const Promise = require('./utils/promise.js');

App({
  onLaunch: function () {
    //强制更新
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function (res) {
        console.log('onCheckForUpdate====', res)
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
          console.log('res.hasUpdate====')
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success: function (res) {
                console.log('success====', res)
                // res: {errMsg: "showModal: ok", cancel: false, confirm: true}
                if (res.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate()
                }
              }
            })
          })
          updateManager.onUpdateFailed(function () {
            // 新的版本下载失败
            wx.showModal({
              title: '已经有新版本了哟~',
              content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
            })
          })
        }
      })
    }

    // 展示本地存储能力
    let that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          that.globalData.code = res.code;
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });

    wx.getSystemInfo({
      success: res => {
        //导航高度
        this.globalData.navHeight = res.statusBarHeight + 120;
        this.globalData.windowHeight = res.windowHeight,
          this.globalData.windowWidth = res.windowWidth
        let clientHeight = res.windowHeight;
        let clientWidth = res.windowWidth;
        let ratio = 750 / clientWidth;
        let height = clientHeight * ratio;
        this.globalData.height = height;

      }, fail(err) {

      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: res => {
              this.globalData.userInfo = res.userInfo
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
          
          // wx.getUserProfile({
          //   desc: '用于完善学员资料',
          //   success: (res) => {
          //     close.log("app.js--getUsereProfile--res="+JSON.stringify(res))
          //     this.globalData.userInfo = res.userInfo
          //     if (this.userInfoReadyCallback) {
          //       this.userInfoReadyCallback(res)
          //     }
         
          //   }
          // })
        }
      }
    })

    //获取屏幕信息
    this.screenSize();
  },

  screenSize: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        var ww = res.windowWidth;
        var hh = res.windowHeight;
        that.globalData.ww = ww;
        that.globalData.hh = hh;
      }
    })
  },

  globalData: {
    key: "yxp",
    appid: "wxebb42ec67a3e805b",
    hashStr: "wx092119292257021779a0eeb63858760852",
    mch_key: "WhVNVFERtbe7vb6n7tvVRb7unt8BNTN9",//key
    mch_id: "1519774121",//mch_id
    trade_type: "JSAPI",//type
    userInfo: null,
    navHeight: 0,
    imageurl: 'https://yxpmaterial.yixuepai100.com/',
    voicepath: 'https://yxpmedia.yixuepai100.com/Public/uploads/american/',
    outUrl: "",
    mp3: {
      review: 'https://yxpmaterial.yixuepai100.com/MP3/baogao.mp3',
      selected: 'https://yxpmaterial.yixuepai100.com/MP3/click.mp3',
      gamesuccess: 'https://yxpmaterial.yixuepai100.com/MP3/success.mp3',
      gamefaild: 'https://yxpmaterial.yixuepai100.com/MP3/fail.wav',
      selectwrong: 'https://yxpmaterial.yixuepai100.com/MP3/selecterr.mp3',
      doright: 'https://yxpmaterial.yixuepai100.com/MP3/selectsucc.mp3',
      wordin:'https://yxpmaterial.yixuepai100.com/word_in.mp3'
    },
    taskTwoMinuts: 0,
    openid: false,
    // apiURL: 'https://e-school.sse100.cn/api/',    ///正式的 
    // apiBaseUrl: 'https://e-school.sse100.cn/',    //正式的 
    apiURL: 'https://e-school.yixuepai100.com/api/',    //正式的备用 
      apiBaseUrl: 'https://e-school.yixuepai100.com/',    //正式的备用
   

    // apiURL: 'https://newyxp.sse100.com/api/',  //测试的
    // apiBaseUrl: 'https://newyxp.sse100.com/',  //测试的

    // apiURL: 'https://yxpsuper.sse100.com/api/',    //预发布版的 
    // apiBaseUrl: 'https://yxpsuper.sse100.com/',    //预发布版
    
    
    code: false,
    studentInfo: false,
    taskTwoUpresult: false,
    spellUpresult: false,
    studyDate: '',
    isUsvoice: 1, // 0为英音，1为美音
  },
  userInfoReadyCallback: function (res) {
    this.globalData.userInfo = res.userInfo
  },//震动
  vibrateShortTap: function () {
    
    if (wx.vibrateShort) {
      wx.vibrateShort();
    }

  },
  bezier: function (pots, amount, isLeft) {
    var pot;
    var lines;
    var ret = [];
    var points;
    for (var i = 0; i <= amount; i++) {
      points = pots.slice(0);
      lines = [];
      while (pot = points.shift()) {
        if (points.length) {
          lines.push(pointLine([pot, points[0]], i / amount));
        } else if (lines.length > 1) {
          points = lines;
          lines = [];
        } else {
          break;
        }
      }
      ret.push(lines[0]);
    }
    function pointLine(points, rate) {
      var pointA, pointB, pointDistance, xDistance, yDistance, tan, radian, tmpPointDistance;
      var ret = [];
      pointA = points[0];//点击
      pointB = points[1];//中间
      if (isLeft) {
        xDistance = pointA.x - pointB.x;
      } else {
        xDistance = pointB.x - pointA.x;
      }
      yDistance = pointB.y - pointA.y;
      pointDistance = Math.pow(Math.pow(xDistance, 2) + Math.pow(yDistance, 2), 1 / 2);
      tan = yDistance / xDistance;
      radian = Math.atan(tan);
      tmpPointDistance = pointDistance * rate;
      let xPoint;
      if (isLeft) {
        xPoint = pointA.x - tmpPointDistance * Math.cos(radian);
      } else {
        xPoint = pointA.x + tmpPointDistance * Math.cos(radian);
      };
      ret = {
        x: xPoint,
        y: pointA.y + tmpPointDistance * Math.sin(radian)
      };
      return ret;
    }
    return {
      'bezier_points': ret
    };
  },
})